package com.demo.shardingsphere.order.config;


import com.demo.shardingsphere.util.OrderUtil;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;


import java.util.Collection;

/**
 * 订单号查询分库路由
 */
public class OrderNoQueryDatabaseAlgorithm implements PreciseShardingAlgorithm<String> {


    /**
     * 分库规则 uid 取模
     * @param availableTargetNames
     * @param shardingValue
     * @return
     */
    @Override
    public String doSharding(Collection<String> availableTargetNames,
                             PreciseShardingValue<String> shardingValue) {
        String orderNo = shardingValue.getValue();
        Integer uidSuffix= OrderUtil.parseUidSuffix(orderNo);
        //分2个库
        String dbName= "db"+uidSuffix/2%2;
        return dbName;

    }
}
